/*****************************************************************************
 *                                                                           *
 *   Copyright (C) 2010 by Luca Cireddu                                      *
 *                         luca@lucacireddu.it                               *
 *                         http://www.lucacireddu.it                         *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.                                          *
 *   51 Franklin Street, Fifth Floor                                         *
 *   Boston, MA  02110-1301, USA.                                            *
 *                                                                           *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "common.h"
#include "datacollect.h"
#include "defines.h"
#include "ngexit.h"
#include "parseargs.h"

extern graph_param params;
extern int grph_x;

data_collect *dataCollect()
{
    data_collect *ret, *first, *last;
    FILE *read;
    time_t read_byte_ts;
    long long unsigned int read_byte_rx, read_byte_tx;
    int i;

    ret = (data_collect *) malloc(sizeof(data_collect));
    first = ret;

    i = 0;

    read = fopen(params.datastorage, "r");

    while((!feof(read)) && (i < grph_x)) {
        fscanf(read, "%ld %lld %lld", &read_byte_ts, &read_byte_rx, &read_byte_tx);
        if(read_byte_ts != 0 && read_byte_rx != 0 && read_byte_tx != 0) {
            if(!feof(read)) {
                ret->id = i;
                ret->byte_ts = read_byte_ts;
                ret->byte_rx = read_byte_rx;
                ret->byte_tx = read_byte_tx;

                if((!feof(read)) && (i < grph_x)) {
                    ret->next = (data_collect *) malloc(sizeof(data_collect));
                    last = ret;
                    ret = ret->next;
                }

                i++;
            }
        } else {
            ngExit("Errore in lettura");
        }
    }

    fclose(read);

    ret->next = first;
    ret = first;

    return ret;
}

data_collect *dataParse(data_collect *input)
{
    data_collect *ret, *first, *last;
    long long unsigned int prev_rx;
    long long unsigned int prev_tx;

    ret = (data_collect *) malloc(sizeof(data_collect));
    first = ret;

    while(input->id != 0)
        input = input->next;

    prev_rx = 0;
    prev_tx = 0;

    do {
        ret->id = input->id;
        ret->byte_ts = input->byte_ts;

        ret->byte_rx = input->byte_rx - prev_rx;
        ret->byte_tx = input->byte_tx - prev_tx;

        prev_rx = input->byte_rx;
        prev_tx = input->byte_tx;

        ret->next = (data_collect *) malloc(sizeof(data_collect));
        last = ret;
        ret = ret->next;

        input = input->next;
    } while(input->id != 0);

    free(ret);
    ret = last;
    ret->next = first;

    return ret;
}

void freeDataCollect(data_collect *data)
{
    data_collect *next;

    while(data->id != 0)
        data = data->next;

    data = data->next;

    do {
        next = data->next;
        if(next != data)
            free(data);
        data = next;
    } while(data->id != 0);

    free(data);
}
